package com.neusoft.databus.core.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.exception.CustomRuntimeException;
import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.databus.common.bean.CommandBean;
import com.neusoft.databus.common.constant.DatabusErrorCode;
import com.neusoft.databus.common.dto.MonitorDataDTO;
import com.neusoft.databus.common.model.MonitorData;
import com.neusoft.databus.common.model.Resource;
import com.neusoft.databus.core.repository.ResourceRepository;

/**
 * description
 *
 * @author sunchf
 * @date 2018年12月24日 上午9:44:01
 */
@Component
@ConditionalOnProperty(name = "databus.core.hbase.enabled", havingValue = "false", matchIfMissing = true)
public class MonitorDataConverter extends AbstractConverter<MonitorData, MonitorDataDTO> {

    @Autowired
    private ResourceRepository resourceRepository;

    @Override
    public MonitorData toModel(MonitorDataDTO dto) {
        final MonitorData model = new MonitorData();
        model.setData(dto.getData());
        model.setCollectedTime(dto.getCollectedTime());
        final String code = dto.getResourceCode();
        final Resource resource = this.resourceRepository.findByCode(code);
        if (null == resource) {
            throw new CustomRuntimeException(DatabusErrorCode.ERR_CODE_404001, new Object[] {code });
        }
        model.setResource(resource);
        return model;
    }

    @Override
    public MonitorDataDTO toDTO(MonitorData model, boolean forListView) {
        final MonitorDataDTO dto = new MonitorDataDTO();
        dto.setId(model.getId());
        dto.setData(model.getData());
        dto.setCollectedTime(model.getCollectedTime());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setResourceCode(model.getResource().getCode());
        dto.setResourceName(model.getResource().getName());
        return dto;
    }

    public List<MonitorData> toModel(CommandBean bean) {

        final List<MonitorData> models = new ArrayList<>();
        bean.getResourceBean().forEach(r -> {
            final Resource resource = this.resourceRepository.findByCode(r.getCode());
            final MonitorData model = new MonitorData();
            model.setResource(resource);
            model.setCollectedTime(bean.getCollectEndTime());

            final Map<String, Object> map = new HashMap<>();
            r.getMetrics().stream().filter(it -> it.isParseSuccess()).forEach(it -> {
                map.put(it.getCode(), it.getValue());
            });

            model.setData(JsonUtils.pojoToJson(map));
            if (!map.isEmpty()) {
                models.add(model);
            }
        });

        return models;
    }

}
